package com.yun.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.blog.bean.Blog;
import com.yun.blog.bean.Category;
import com.yun.blog.bean.TagRelation;
import com.yun.blog.bean.dto.BlogDto;
import com.yun.blog.dao.BlogMapper;
import com.yun.blog.service.BlogService;
import com.yun.blog.service.TagRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * title        : BlogServiceImpl
 * projectName  : dantouBlog-back
 * description  : TODO
 * author       : simple
 * date         : 2022/11/23下午10:09
 */

@Service
public class BlogServiceImpl
        extends ServiceImpl<BlogMapper, Blog>
        implements BlogService {

    @Resource
    BlogMapper blogMapper;

    @Resource
    TagRelationService tagRelationService;

    @Override
    @Cacheable(value = "blog", key = "#blogId")
    public Blog getByBlogId(Integer blogId) {
        return blogMapper.getByBlogId(blogId);
    }

    @Override
    public <P extends IPage<Blog>> P customPage(P page, String searchText, String orderBy) {
        return blogMapper.customPage(page, searchText, orderBy);
    }

    @Override
    public <P extends IPage<Blog>> P timelinePage(P page) {
        return blogMapper.timelinePage(page);
    }

    @Override
    public List<Blog> listAllByBlogCategoryId(Integer blogCategoryId) {
        return blogMapper.listAllByBlogCategoryId(blogCategoryId);
    }

    @Override
    public List<Blog> listAllByTagId(Integer tagId) {
        return blogMapper.listAllByTagId(tagId);
    }

    @Override
    public List<Blog> getByIsTop(Boolean isTop) {
        return blogMapper.getByIsTop(isTop);
    }

    @Override
    public List<Blog> getNewBlogByCount(Integer count) {
        return blogMapper.getNewBlogByCount(count);
    }

    @Override
    public int addBlogViews(Integer blogId) {
        return blogMapper.addBlogViews(blogId);
    }

    @Override
    public <P extends IPage<Blog>> P getByCondition(P page, String searchText, Integer categoryId, Boolean isTop, Collection<Integer> tagIdList, String beginCreateTime, String endCreateTime, String orderBy) {
        return blogMapper.getByCondition(page, searchText, categoryId, isTop, tagIdList, beginCreateTime, endCreateTime, orderBy);
    }


    @Override
    public Integer getViewCount() {
        return blogMapper.getViewCount();
    }

    @Override
    public Integer getCountByCategory(Category category) {
        return blogMapper.getCountByCategoryId(category.getCategoryId());
    }

    @Override
    @Cacheable(value = "viewRank", key = "#count")
    public List<Blog> getViewRank(int count) {
        return blogMapper.getViewRank(count);
    }


    @Override
    @Transactional
    @CachePut(value = "blog", key = "#result.blogId")
    public Blog saveOrUpdate(BlogDto blogDto) {
        // 使用dto对象构建一个blog对象，相同属性赋值
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogDto, blog);
        // id存在就修改，不存在就新增
        if (blog.getBlogId() == null) {
            blogMapper.insert(blog);
        } else {
            blogMapper.updateById(blog);
        }
        // TODO 这个标签关系修改的很糟糕，有空再优化
        // 得到标签id
        List<Integer> tags = blogDto.getTagIds();
        // 如果有博客id，删除所有的此博客以前的tag关系
        if (blog.getBlogId() != null) {
            tagRelationService.delByBlogId(blog.getBlogId());
        }
        // 保存标签关系
        if (tags != null) {
            for (Integer tag : tags) {
                TagRelation tagRelation = new TagRelation();
                tagRelation.setBlogId(blog.getBlogId());
                tagRelation.setTagId(tag);
                tagRelationService.save(tagRelation);
            }
        }
        return blog;
    }

    @Override
    public Blog getPrevious(Integer blogId) {
        return blogMapper.getPrevious(blogId);
    }

    @Override
    public Blog getNext(Integer blogId) {
        return blogMapper.getNext(blogId);
    }

    /**
     * 获取没标记被删除的文章的数量
     *
     * @return long
     * @author yun
     * @date 2023/6/22 下午10:26
     */
    @Override
    public long count() {
        LambdaQueryWrapper<Blog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Blog::getIsDeleted, false);
        return super.count(queryWrapper);
    }
}
